package com.wewins.fota.common.util;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class CommonUtil {

    public static List<String> getRstStrings(BindingResult bindingResult) {
        return bindingResult.getAllErrors()
                .stream()
                .map(item -> {
                    if (item instanceof FieldError) {
                        return item.getObjectName() + "." + ((FieldError) item).getField() + ":" + item.getDefaultMessage();
                    } else {
                        return item.toString();
                    }
                })
                .collect(Collectors.toList());
    }

    /**
     * 时分秒毫秒级别的大小比对，忽略年月日；
     *
     * @param dateMillis1 区间值/比较值
     * @param dateMillis2 区间值/比较值
     * @return 1 表示起始大于终止，-1 表示终止大于起始，0 表示相等
     */
    public static int compareInnerDay(long dateMillis1, long dateMillis2) {
        Calendar date1 = Calendar.getInstance();
        date1.setTimeInMillis(dateMillis1);
        Calendar date2 = Calendar.getInstance();
        date2.setTimeInMillis(dateMillis2);

        if (date1.get(Calendar.HOUR_OF_DAY) > date2.get(Calendar.HOUR_OF_DAY)) {
            return 1;
        }
        if (date1.get(Calendar.HOUR_OF_DAY) < date2.get(Calendar.HOUR_OF_DAY)) {
            return -1;
        }

        if (date1.get(Calendar.MINUTE) > date2.get(Calendar.MINUTE)) {
            return 1;
        }
        if (date1.get(Calendar.MINUTE) < date2.get(Calendar.MINUTE)) {
            return -1;
        }

        if (date1.get(Calendar.SECOND) > date2.get(Calendar.SECOND)) {
            return 1;
        }
        if (date1.get(Calendar.SECOND) < date2.get(Calendar.SECOND)) {
            return -1;
        }

        return Integer.compare(date1.get(Calendar.MILLISECOND), date2.get(Calendar.MILLISECOND));
    }


    public static boolean equalMap(Map<String, String> map1, Map<String, String> map2) {
        if (map1.size() != map2.size()) {
            return false;
        }
        for (Map.Entry<String,String> item : map1.entrySet()) {
            if (!item.getValue().equals(map2.get(item.getKey()))) {
                return false;
            }
        }
        return true;
    }

    public static boolean isEmptyMap(Map<?, ?> map) {
        return map == null || map.size() == 0;
    }
}
